import base64
import lz4.block as lb
import numpy as np
import bitMap
from bokeh.models import (ColumnDataSource, Line)
from bokeh.plotting import figure


class DrawLine(object):

    def __init__(self):
        pass

    def __del__(self):
        pass

    # @classmethod
    # def big_small_end_convert(cls, data):
    #     return binascii.hexlify(binascii.unhexlify(data)[::-1])

    @classmethod
    def drawline(cls, map_dict, plot: figure, path_str: str, path_style: str, current_path_len: int):
        if len(path_str) == 0:
            return
        cur_path_data = base64.b64decode(path_str)
        data_header = cur_path_data[:44]
        # headerLen: 0, version: 0, pathId: 0, dataLen: 0, startPos: 0, initX: 0, initY: 0, curX: 0, curY: 0, curPhi: 0
        header_map_array = np.frombuffer(data_header, dtype=np.int32)

        data_len = header_map_array[3]
        start_x = header_map_array[5]
        start_y = header_map_array[6]

        data_body = cur_path_data[45:]
        alldata_len = int(data_len + data_len/4)

        body_map_data = lb.decompress(data_body, uncompressed_size=alldata_len)

        path_body = body_map_data[:data_len]
        type_body = body_map_data[data_len:]

        path_array = np.frombuffer(path_body, dtype=np.int16)
        line_num = int(len(path_array) / 2)
        path_point_array = path_array.reshape(line_num, 2)

        # 点位坐标
        p_x = []
        p_y = []
        # map_v = bitMap.currentMap
        point_x = start_x
        point_y = start_y
        for point in path_point_array:
            # w_x = m_x * resolution + min_x
            print(point)
            x = point[0]
            y = point[1]
            print(f"x: {x}   y: {y}")
            point_x += x
            point_y += y
            print(f"pointX: {point_x}   pointY: {point_y}")
            map_x = (point_x / 1000 - map_dict.get("x_min")) / map_dict.get("resolution")
            map_y = (point_y / 1000 - map_dict.get("y_min")) / map_dict.get("resolution")
            print(f"map_x: {int(map_x)}   map_y: {int(map_y)}")
            print("==========================================================")
            p_x.append(int(map_x))
            p_y.append(int(map_y))
        # 画线
        lines_source = ColumnDataSource(data=dict(x=p_x, y=p_y))
        line = Line(x='x', y='y', line_color="red", line_width=bitMap.MapScale)
        plot.add_glyph(lines_source, line)
        # 画主机
        if (path_style == 'history' and current_path_len == 0) \
                or (path_style == 'current' and current_path_len != 0):
            if len(p_x) != 0 and len(p_y) != 0:
                cleaner_x = p_x[len(p_x) - 1]
                cleaner_y = p_y[len(p_y) - 1]
                # plot.diamond_cross(x=[cleaner_x], y=[cleaner_y], size=10,
                # color="#4169E1", fill_color=None, line_width=2), fill_alpha=0.5
                plot.circle_y(x=cleaner_x, y=cleaner_y, size=15,
                              color="#000000", fill_alpha=0.6, line_width=1)

        return plot
